/**
 * Created by wady on 2017/9/18.
 */
'use strict';
angular.module('backofficeApp.communityAmbassadorManager.controller', []).filter('object2Array', function () {
    return function (input) {
        var out = [];
        for (i in input) {
            out.push(input[i]);
        }
        return out;
    }
}).controller('communityAmbassadorCtrl', ['$scope', '$http', '$compile', function ($scope, $http, $compile) {

    $scope.commissionH5Flags = [
        {key : "1", value : "区间访问"},
        {key : "2", value : "一直可以访问"},
        {key : "3", value : "禁止访问"}
    ];

    $scope.types = [
        {type : "1", name : "注册"},
        {type : "2", name : "忘记密码"},
        {type : "4", name : "修改密码"}
    ];

    /** CACHE: This is the heart of the web page
     *
     *  数据结构: { comm1:{date1:{users:10,vusers:5,questions:30,newusers:...},date2:{users:20,...},...}, comm2:{date1:{users:30,...},...}, comm3:{...}, ...}
     *  e.g. $scope.reportingData[comm1][date2][users] = 20
     */
    var graphColumns = {
        users: '总用户数',
        vusers: '总V用户数',
        question: '发布数',
        questionusers: '发布人数',
        replies: '回复数',
        repliesusers: '回复人数'
    }
    $scope.commsInGraph = {}

    $scope.mainRealUsersSwitch = true;

    /*  宝宝树66小区二次改造 */
    /* 生成小区大使信息Excel */
    $scope.exportModeratorManagerDataInfo1 = function() {
        var startDateSelect = $("#startDateSelect").val();
        if(startDateSelect == '') {
            alert('日期不能为空！');
            return;

        }
            $(".alert").css("display","block");
        $("#submitModeratorManagerInfoData").attr('disabled',"true"); // 添加disabled属性
        var params = {'startDateSelect': startDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/manualmtmtOrderSchedule', params).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoData").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("同步成功件数：" + data);
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoData").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };

    $scope.exportModeratorManagerDataInfo2 = function() {
        var fromDateSelect = $("#fromDateSelect").val();
        if(fromDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var toDateSelect = $("#toDateSelect").val();
        if(toDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }

        if (fromDateSelect > toDateSelect)
        {
            alert('开始日期不能大于结束日期');
            return;
        }

        $(".alert").css("display","block");
        $("#submitModeratorManagerInfoDataByFromTo").attr('disabled',"true"); // 添加disabled属性
        var params = {'fromDateSelect': fromDateSelect, 'toDateSelect': toDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/manualmtmtOrderScheduleFromTo', params).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoDataByFromTo").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("同步成功件数：" + data);
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoDataByFromTo").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };


    $scope.exportModeratorManagerDataInfoRough = function() {
        var roughFromDateSelect = $("#roughFromDateSelect").val();
        if(roughFromDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var roughToDateSelect = $("#roughToDateSelect").val();
        if(roughToDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }

        if (roughFromDateSelect > roughToDateSelect)
        {
            alert('开始日期不能大于结束日期');
            return;
        }

        $(".alert").css("display","block");
        $("#submitModeratorManagerInfoDataByFromToRough").attr('disabled',"true"); // 添加disabled属性
        var params = {'fromDateSelect': roughFromDateSelect, 'toDateSelect': roughToDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/manualmtmtOrderScheduleFromToRough', params).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToRough").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("同步成功天数：" + data);
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToRough").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };

    $scope.exportModeratorManagerDataInfoCheck = function() {
        var checkFromDateSelect = $("#checkFromDateSelect").val();
        if(checkFromDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var checkToDateSelect = $("#checkToDateSelect").val();
        if(checkToDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }

        if (checkFromDateSelect > checkToDateSelect)
        {
            alert('开始日期不能大于结束日期');
            return;
        }

        $(".alert").css("display","block");
        $("#submitModeratorManagerInfoDataByFromToCheck").attr('disabled',"true"); // 添加disabled属性
        var params = {'fromDateSelect': checkFromDateSelect, 'toDateSelect': checkToDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/manualmtmtOrderScheduleFromToCheck', params).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToCheck").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("复核成功件数：" + data);
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToCheck").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };

    $scope.exportModeratorManagerDataInfoActuary = function() {
        var actuaryFromDateSelect = $("#actuaryFromDateSelect").val();
        if(actuaryFromDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var actuaryToDateSelect = $("#actuaryToDateSelect").val();
        if(actuaryToDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }

        if (actuaryFromDateSelect > actuaryToDateSelect)
        {
            alert('开始日期不能大于结束日期');
            return;
        }

        $(".alert").css("display","block");
        $("#submitModeratorManagerInfoDataByFromToActuary").attr('disabled',"true"); // 添加disabled属性
        var params = {'fromDateSelect': actuaryFromDateSelect, 'toDateSelect': actuaryToDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/manualmtmtOrderScheduleFromToActuary', params).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToActuary").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("同步成功天数：" + data);
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoDataByFromToActuary").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };

    $scope.exportRefundStatusFor66 = function() {
        var refundFromDateSelect = $("#refundFromDateSelect").val();
        if(refundFromDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var refundToDateSelect = $("#refundToDateSelect").val();
        if(refundToDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }

        if (refundFromDateSelect > refundToDateSelect)
        {
            alert('开始日期不能大于结束日期');
            return;
        }

        $(".alert").css("display","block");
        $("#submitRefundStatusFor66ByFromTo").attr('disabled',"true"); // 添加disabled属性
        var params = {'fromDateSelect': refundFromDateSelect, 'toDateSelect': refundToDateSelect};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/mtOrder/refundStatusFor66ByStartEndDate', params).then(
            function successCallback(response) {
                $("#submitRefundStatusFor66ByFromTo").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(!data) {
                    data = 0
                }
                alert("退款件数：" + data);
            }, function errorCallback(response) {
                $("#submitRefundStatusFor66ByFromTo").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('同步失败!');
            })
    };


    $scope.getCommissionH5Flag = function() {
        $(".alert").css("display","block");
        var params = {};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/getCommissionH5Flag', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.setCommissionH5Flag = function() {
        $(".alert").css("display","block");
        var setValue = $("#selectedCommissionH5Flag").val();
        var params = {"setValue" : setValue};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/setCommissionH5Flag', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };


    $scope.getCommissionH5UserFlag = function() {
        $(".alert").css("display","block");
        var params = {};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/getCommissionH5UserFlag', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.setCommissionH5UserFlag = function() {
        $(".alert").css("display","block");
        var setValue = $("#selectedCommissionH5UserFlag").val();
        var params = {"setValue" : setValue};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/setCommissionH5UserFlag', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };




    $scope.isAllowCommissionH5User = function() {
        $(".alert").css("display","block");
        var userId = $("#currentUserId").val();
        var params = {"userId" : userId};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/isAllowCommissionH5User', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('请输入合法的用户ID!');
                    return;
                } else {
                    var data = response.data.data;
                    alert("返回数据：" + data);
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.addAllowCommissionH5User = function() {
        $(".alert").css("display","block");
        var userId = $("#currentUserId").val();
        var params = {"userId" : userId};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/addAllowCommissionH5User', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('请输入合法的用户ID!');
                    return;
                } else {
                    var data = response.data.data;
                    alert("返回数据：" + data);
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };


    $scope.removeAllowCommissionH5User = function() {
        $(".alert").css("display","block");
        var userId = $("#currentUserId").val();
        var params = {"userId" : userId};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/removeAllowCommissionH5User', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('请输入合法的用户ID!');
                    return;
                } else {
                    var data = response.data.data;
                    alert("返回数据：" + data);
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.getAllAllowCommissionH5User = function() {
        $(".alert").css("display","block");
        var params = {};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/getAllAllowCommissionH5User', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.clearAllowCommissionH5User = function() {
        $(".alert").css("display","block");
        var params = {};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/clearAllowCommissionH5User', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回数据：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };


    $scope.isAllowCommissionH5UserList = function() {
        $(".alert").css("display","block");
        var userIdList = $("#currentUserIdList").val();
        var params = {"userIdList" : userIdList};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/isAllowCommissionH5UserList', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('请输入合法的用户ID!');
                    return;
                } else {
                    var data = response.data.data;
                    if(data) {
                        alert("存在数据:" + data.exitsArray + '\n' + "不存在数据:" + data.notExitsArray);
                    } else {
                        alert("返回数据：" + null);
                    }
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.addAllowCommissionH5UserList = function() {
        $(".alert").css("display","block");
        var userIdList = $("#currentUserIdList").val();
        var params = {"userIdList" : userIdList};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/addAllowCommissionH5UserList', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('有存在的用户ID,不可添加!');
                    return;
                } else {
                    var data = response.data.data;
                    alert("返回数据：" + data);
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };


    $scope.removeAllowCommissionH5UserList = function() {
        $(".alert").css("display","block");
        var userIdList = $("#currentUserIdList").val();
        var params = {"userIdList" : userIdList};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/removeAllowCommissionH5UserList', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                if(response && response.data && response.data.result && response.data.result.status == '0') {
                    alert('请输入合法的用户ID!');
                    return;
                } else {
                    var data = response.data.data;
                    alert("返回数据：" + data);
                }
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };

    $scope.getSmsVerificationCode = function() {
        $(".alert").css("display","block");
        var mobilePhone = $("#mobilePhone").val();
        var type = $scope.selectedType;
        if(!mobilePhone) {
            alert("手机号不能为空!");
            $(".alert").css("display","none");
            return;
        }
        if(mobilePhone.length != 11) {
            alert("请输入11位手机号!");
            $(".alert").css("display","none");
            return;
        }
        if(!type) {
            alert("类型不能为空!");
            $(".alert").css("display","none");
            return;
        }
        if(type.length != 1) {
            alert("请输入1位类型!");
            $(".alert").css("display","none");
            return;
        }
        var params = {"mobilePhone" : mobilePhone, "type" : type};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/getSmsVerificationCode', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("验证码：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };


    $scope.getRedisValueByKey = function() {
        $(".alert").css("display","block");
        var redisKey = $("#redisKey").val();
        if(!redisKey) {
            alert("redisKey不能为空!");
            $(".alert").css("display","none");
            return;
        }
        var params = {"redisKey" : redisKey};
        $http.post('http://' + backOfficeHost + '/rest/v1.0/operation/getRedisValueByKey', params).then(
            function successCallback(response) {
                $(".alert").css("display","none");
                var data = response.data.data;
                alert("返回值：" + data);
            }, function errorCallback(response) {
                $(".alert").css("display","none");
                alert('失败!');
            })
    };
}])